<template>
	<view class="container">
		<HeaderBar :opacity="0" :isGoBack="true" :lazy="true" @goBackFn="goBackFn" />
		<swiper class="swiper" @change="change" :current="tabIndex" :disable-touch="true">
			<swiper-item v-for="it in tab">
				<view class="bg-img">
					<image :src="it.background" mode="widthFix"></image>
				</view>
			</swiper-item>
		</swiper>

		<view class="circle-type">
			<view class="flex-row title-container tab">
				<view class="flex-1 flex-row-center title-container-item " v-for="(it,index) in tab" :key="it.value">
					<view :class="{'tab-tx': true, tabAc: tabIndex === index}" @click="setTabIndex(index)"
						:style="{borderLeft: index === 0 ? 'none' : ''}">{{ it.name }}</view>

				</view>
			</view>
			<view class="content">
				<view class="separator"></view>
				<view class="content-img">
					<image :src="tab[tabIndex].img" mode="widthFix"></image>
				</view>
				<view class="create-btn" @click="open">{{cid?'续费圈子':'创建圈子'}}</view>
			</view>
		</view>

		<!-- 购买弹窗 -->
		<u-popup :show="show" mode="bottom" @close="close" style="position: relative;">
			<scroll-view scroll-y="true" class="popup-y">
				<view style="padding: 30rpx;">
					<text class="title">圈子服务</text>
					<view class="price" style="margin-top: 20rpx;">
						<view class="pay-container flex-1" :class="{ acitve: currentType === index }"
							@click="setCurrentType(index)" v-for="(it,index) in payList">
							<image :src="it.price_img" mode="widthFix"></image>
						</view>
					</view>
					<view class="flex-row-center" style="margin: 30rpx 0;">
						<u-checkbox activeColor="#67b12f" shape="circle" @change="changeCheck"></u-checkbox>
						<text @click="openPopup()">购买表示你已经同意服务协议</text>
					</view>
					<view class="placeholder"></view>
				</view>
			</scroll-view>
			<view class="pay-bottom">
				<view class="pay-botton" @click="buy">立即购买</view>
			</view>
		</u-popup>
		<PrivacyAgreement ref="privacyAgreementRef" />
	</view>
</template>

<script>
	import {
		circle_right,
		circle_price,
		circle_pay,
		circle_buy
	} from '@/common/http/api.js'
	import HeaderBar from "@/components/header/header.vue";
	import PrivacyAgreement from '@/components/privacy-agreement/privacy-agreement.vue'
	export default {
		components: {
			PrivacyAgreement,
			HeaderBar
		},
		data() {
			return {
				show: false,
				checked: false,
				currentType: 0,
				tab: [
					// {
					//   name: '兴趣圈'
					// }
				],
				tabIndex: 0,
				payList: [],
				right: {},
				cid: '',
				checked: false
			}
		},
		onLoad(e) {
			if (e.cid) {
				this.cid = e.cid
				if (e.ctype == 3) {
					this.tabIndex = 1
				} else if (e.ctype == 2) {
					this.tabIndex = 0
				} else if (e.ctype == 1) {
					this.tabIndex = 2
				}
			}
			circle_right().then(res => {

				this.tab = res.data.filter(i => i.id != 5)
			})
		},
		methods: {
			goBackFn() {
				uni.navigateBack()
			},
			stopTouchMove() {
				// if(this.cid){
				// 	return false
				// }
			},
			openPopup() {
				this.$refs.privacyAgreementRef.show(this.term)
			},
			changeCheck(e) {
				this.checked = e
				console.log('e', e)
			},
			setCurrentType(index) {
				this.currentType = index

			},
			open() {
				//判断是不是vip
				if (this.tabIndex === 2) {
					uni.navigateTo({
						url: '/pagesA/createCircle/createCircle?type=' + 1
					})
				} else {
					uni.showLoading()
					// 2 企业 3组织
					circle_buy({
						type: this.tabIndex === 0 ? 1 : 2
					}).then(res => {
						uni.hideLoading()
						if (Array.isArray(res.data) && res.data.length > 0) {
							uni.navigateTo({
								url: '/pagesA/createCircle/createCircle?type=' + (this.tabIndex === 0 ? 2 :
									3)
							})
						} else {
							this.show = true
							circle_price({
								id: this.tab[this.tabIndex].id
							}).then(res => {
								this.payList = res.data.price ?? []
								this.right = res.data.right ?? {}
								this.term = res.data.term
							})
						}

					})

				}

			},
			change(e) {
				if (this.cid) {
					return
				}
				this.tabIndex = e.detail.current
			},
			setTabIndex(index) {
				if (this.cid) {
					return
				}
				this.tabIndex = index
			},
			close() {
				this.show = false
			},
			buy() {
				if (!this.checked) {
					this.$u.toast('请先勾选服务协议！')
					return
				}
				let type = ''
				switch (this.tab[this.tabIndex].name) {
					case '兴趣':
						type = 1
						break;
					case '组织':
						type = 3
						break;
					case '企业':
						type = 2
						break;
					default:
						type = 1
						break;
				}

				let current = this.payList[this.currentType]
				uni.showLoading({
					mask: true,
					title: '支付中'
				})

				circle_pay({
					circle_id:this.cid,
					id: current.id
				}).then(res => {
					uni.hideLoading()
					//调用微信官方支付接口弹出付款界面,输入密码扣款
					wx.requestPayment({
						timeStamp: String(res.data.timeStamp),
						nonceStr: res.data.nonceStr,
						package: res.data.package,
						signType: res.data.signType,
						paySign: res.data.paySign,
						success: res => {
							uni.showToast({
								duration: 3000,
								title: '支付成功',
								icon: 'none'
							})
							setTimeout(() => {
								if(this.cid){
									uni.navigateBack()
								}else{
									uni.navigateTo({
										url: '/pagesA/createCircle/createCircle?type=' +
											type
									})
								}
							
							}, 1500)

						},
						fail: function(err) {
							console.log('支付失败', err);
						}
					})
				})

			}
		}
	}
</script>

<style lang='scss' scoped>
	.swiper {
		width: 100%;
		height: 100vh;
		position: fixed;
		top: 0;
		left: 0;
	}

	.container {
		min-height: 100vh;
		position: relative;

		.bg-img {
			width: 100%;

			image {
				width: 100%;
			}
		}

		.pay-container {
			border-radius: 20rpx;
			overflow: hidden;
			box-shadow: 0rpx 6rpx 15rpx #f3f0e7;

			// height: 200rpx;
			&.acitve {

				box-shadow: 0rpx 6rpx 15rpx #df614d;
			}
		}

		.pay-bottom {
			position: absolute;
			bottom: 0;
			left: 0;
			width: 100%;
			padding: 20rpx 40rpx 40rpx;
			background-color: #fff;
			box-sizing: border-box;

			.discounts {
				font-size: 26rpx;
				color: #d68238;
			}

			.pay-coupon {
				font-size: 20rpx;
				color: #777777;
				margin: 10rpx 0;
			}

			.pay-botton {
				width: 100%;
				padding: 20rpx 0;
				text-align: center;
				background-color: #dd5845;
				border-radius: 50rpx;
				color: #fff;
			}
		}

		.placeholder {
			height: 200rpx;
		}

		.table {
			border-radius: 20rpx;
			overflow: hidden;
			box-shadow: -10px 0 10px -5px #f8f8f8,
				/* 左侧阴影 */
				10px 0 10px -5px #f8f8f8,
				/* 右侧阴影 */
				0 10px 10px -5px #f8f8f8;

			/* 底部阴影 */
			.cell {
				padding: 20rpx;

				.right {
					width: 60rpx;
					text-align: center;
					color: #ab9871;
				}

				.wrong {
					width: 60rpx;
					text-align: center;
					color: #aeaeae;
				}
			}

		}

		.title {
			font-size: 35rpx;
			font-weight: bold;
		}

		.pay {
			width: 100%;
			padding: 20rpx;
			background-color: #fff;
			box-sizing: border-box;

			&-time {
				font-size: 30rpx;
				font-weight: bold;
				color: #d17d68;
			}

			&-price {
				font-size: 40rpx;
				font-weight: bold;
				color: #d17d68;
				margin: 10rpx 0;
			}

			&-price.acitve {
				color: #df614d;
			}

			&-calc {
				font-size: 20rpx;
				color: #d18370;
			}

			&-tag {
				width: 100%;
				background-color: #f3f0e7;
				padding: 10rpx 0;
				font-size: 20rpx;
			}

			&-tag.acitve {
				background-color: #df614d;
				color: #fff;
			}
		}

		.circle-type {
			position: absolute;
			top: 450rpx;
			left: 50%;
			transform: translateX(-50%);
			width: 90%;
			border-radius: 20rpx;
			overflow: hidden;
			padding-bottom: 30rpx;

			.content {
				background-color: #fff;
				padding-top: 20rpx;
				padding-bottom: 20rpx;

				.create-btn {
					width: calc(100% - 40rpx);
					margin-left: 20rpx;
					padding: 20rpx 0;
					text-align: center;
					background-color: #6aaf74;
					border-radius: 50rpx;
					color: #fff;
				}

				.right {
					width: 100rpx;
					height: 60rpx;
					line-height: 60rpx;
					margin-top: 20rpx;
					text-align: center;
					background-color: #fbfbfb;
					color: #ab9871;
				}

				.wrong {
					width: 100rpx;
					height: 60rpx;
					line-height: 60rpx;
					margin-top: 20rpx;
					text-align: center;
					background-color: #fbfbfb;
					color: #aeaeae;
				}

				.content-text {
					padding: 20rpx;

					.content-title {
						font-size: 30rpx;
					}

					.equity {
						line-height: 80rpx;
						font-size: 28rpx;
						font-weight: bold;
					}
				}

				.separator {
					width: 100%;
					height: 10rpx;
					background-color: #fbfbfb;
				}
			}

			.title-container {
				background-color: white;

				&-item {
					padding: 20rpx 0;
				}
			}
		}
	}

	.tab {
		//background: rgba();
		background: linear-gradient(to right, rgba(255, 255, 255, 0), #fff) !important;
		overflow: hidden;
		display: flex;

		&>view {
			position: relative;
			height: 41rpx;
			//width: 112rpx;
			flex: 1;
		}

		.tabAc {
			background: white;
			/* tab高度 */
			--height: 36px;
			/* 弧度半径控制曲率 */
			--r: 8px;
			/* 边框遮罩 */
			--mask: radial-gradient(var(--r) at var(--r) 0, #0000 100%, #000 100%) calc(-1*var(--r)) 100%/100% var(--r) repeat-x, linear-gradient(#000, #000) padding-box;
			width: fit-content;
			height: var(--height);
			/* 靠border延伸出背景色，这里border设置透明也行，设置tab背景色也行，但是设置透明可以就不用后续调整了 */
			border-left: var(--r) solid #0000;
			border-right: var(--r) solid #0000;
			/* 给 border-radius 让padding-box上边具有弧度 */
			border-radius: calc(2*var(--r)) calc(2*var(--r)) 0 0/var(--r);
			/* 设置蒙层，注意要同时设置带-webkit-前缀的 */
			mask: var(--mask);
			-webkit-mask: var(--mask);

			.tab-tx {
				font-weight: bold;
			}
		}

		.tab-tx {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			z-index: 2;
			box-sizing: border-box;
		}

	}

	.content-img {
		width: 100%;

		image {
			width: 100%;
		}
	}

	.popup-y {
		max-height: 70vh;
	}

	.price {
		overflow-x: scroll;
		white-space: nowrap;

		.pay-container {
			//width: 300rpx;
			//min-width: 100rpx;
			width: 215rpx;
			display: inline-block;
			margin-right: 20rpx;
			line-height: 26rpx;

			//max-width: 33%;
			image {
				width: 100%;
			}
		}
	}
</style>